<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 * App\Models\GoodsImage
 *
 * @property int $goods_image_id 主键 自增 无意义
 * @property int $goods_id 商品goods(spu)表的外键
 * @property string|null $image spu轮播图片
 * @property int $is_default 是否是默认主图 0-否 1-是
 * @property int $disabled 删除状态 0-未删除 1-删除
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\GoodsImage whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\GoodsImage whereDisabled($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\GoodsImage whereGoodsId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\GoodsImage whereGoodsImageId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\GoodsImage whereImage($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\GoodsImage whereIsDefault($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\GoodsImage whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class GoodsImage extends Model
{
    protected $table = 'goods_image';
    protected $primaryKey = 'goods_image_id';

    protected $guarded = [];

    /**获取商品轮播图
     * @param $spu_id
     */
    public static function api_get_goods_banner($spu_id)
    {
        $query = self::query()
            -> select('image')

            -> where('goods_id','=',$spu_id)
            -> where('disabled','=',0)
            -> orderBy('is_default','desc');
       self::selectImage($query);
       return $query-> get()-> toArray();
    }

    /**
     * @param $query \Illuminate\Database\Eloquent\Builder
     */
    public static function selectImage($query)
    {
        $query -> selectRaw(DB::raw('CONCAT("'.''.'",image) AS image')->getValue());
    }
}
